#include "pcm_eq_api.h"
/* #include "resample_api.h" */
/* #include "pcm_eq.h" */
/* #include "resample.h" */
#include "decoder_api.h"
/* #include "config.h" */
#include "sound_mge.h"
#include "app_modules.h"


#define LOG_TAG_CONST       NORM
#define LOG_TAG             "[normal]"
#include "log.h"

#if defined(PCM_SW_EQ_EN) && (PCM_SW_EQ_EN)

static u32 g_work_buf[660 / 4] AT(.pcm_eq_data);

const int *get_pcm_eq_tab(u32 sr);

PCM_EQ_PARM parm;
void *pcm_eq_api(void *obuf, u32 sr, u32 channel, void **ppsound)
{

    memset(&parm, 0, sizeof(PCM_EQ_PARM));
    parm.nSection = 10;

    u32 buf_size = pcm_eq_buf_len(parm.nSection, channel);

    if (sizeof(g_work_buf) < buf_size) {
        log_info("PCM EQ Work Data space is not big enough:%d : %d", sizeof(g_work_buf), buf_size);
        return NULL;
    }
    log_info("PCM EQ Work Data space is enough:%d : %d", sizeof(g_work_buf), buf_size);

    parm.LCoeff_OnChip = get_pcm_eq_tab(sr);
    if (1 < channel) {
        parm.RCoeff_OnChip = get_pcm_eq_tab(sr);
    }
    parm.LGain = 1 << 20;
    parm.RGain = 1 << 20;
    parm.SHI = 10;
    parm.SHO = 8;
    parm.channel = channel;
    return pcm_eq_phy(obuf, &g_work_buf[0], &parm, ppsound);
}

void *link_pcm_eq_sound(void *p_sound_out, void *p_dac_cbuf, void **pp_effect, u32 sr, u32 channel)
{
    sound_out_obj *p_next_sound = 0;
    sound_out_obj *p_curr_sound = p_sound_out;
    p_curr_sound->effect = pcm_eq_api(p_curr_sound->p_obuf, sr, channel, (void **)&p_next_sound);
    if (NULL != p_curr_sound->effect) {
        if (NULL != pp_effect) {
            *pp_effect = p_curr_sound->effect;
        }
        p_curr_sound->enable |= B_DEC_EFFECT;
        p_curr_sound = p_next_sound;
        p_curr_sound->p_obuf = p_dac_cbuf;
    } else {
        log_info("pcm eq init fail\n");
    }
    log_info("pcm eq init succ\n");
    return p_curr_sound;
}




/*
 * AUTO GENERATED AT 2021-07-13T16:15:06
 * USING EQTools V3.1.7 beta  (@20200516.2)
 */

static const int eq_filt_44100[] = {
    4194304, -4173016, 4173016, 8367268, -8367268, // seg 0
    4194304, -4149302, 4149302, 8343368, -8343368, // seg 1
    4294254, -4127248, 3933563, 8319444, -8125808, // seg 2
    4194304, -3994340, 3994340, 8183858, -8183858, // seg 3
    4443988, -3904429, 3449405, 8071002, -7617537, // seg 4
    4194304, -3365194, 3365194, 7469851, -7469851, // seg 5
    4831567, -3165399, 2194686, 7037045, -6108889, // seg 6
    5667101, -2169436, 515593, 4557489, -3373064, // seg 7
    4694807, -1049027, 490047, 1389878, -1241706, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_22050[] = {
    4194304, -4151836, 4151836, 8345928, -8345928, // seg 0
    4194304, -4104782, 4104782, 8298140, -8298140, // seg 1
    4392568, -4061289, 3688662, 8247227, -7874978, // seg 2
    4194304, -3803905, 3803905, 7979513, -7979513, // seg 3
    4675449, -3635710, 2829933, 7722952, -6928192, // seg 4
    4194304, -2699945, 2699945, 6569154, -6569154, // seg 5
    5290310, -2424727, 1053447, 5483731, -4347654, // seg 6
    6104494, -1568088, -235053, 148584, -102090, // seg 7
    4516387, -2170262, 1716378, -5470155, 5080055, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_11025[] = {
    4194304, -4109797, 4109797, 8303258, -8303258, // seg 0
    4194304, -4017169, 4017169, 8207728, -8207728, // seg 1
    4584260, -3932683, 3241369, 8094063, -7405549, // seg 2
    4194304, -3449817, 3449817, 7572730, -7572730, // seg 3
    5085702, -3159420, 1870493, 6954275, -5735363, // seg 4
    4194304, -1742151, 1742151, 4843131, -4843131, // seg 5
    5789298, -1619077, 17448, 2166975, -1569958, // seg 6
    4194304, 0, 0, 0, 0, // seg 7
    4194304, 0, 0, 0, 0, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_48000[] = {
    4194304, -4174742, 4174742, 8369001, -8369001, // seg 0
    4194304, -4152940, 4152940, 8347043, -8347043, // seg 1
    4286193, -4132656, 3954139, 8325179, -8146700, // seg 2
    4194304, -4010225, 4010225, 8200481, -8200481, // seg 3
    4424387, -3927186, 3504842, 8098014, -7676891, // seg 4
    4194304, -3425961, 3425961, 7543962, -7543962, // seg 5
    4786894, -3237528, 2317510, 7156490, -6270558, // seg 6
    5591782, -2272987, 656705, 4905651, -3679648, // seg 7
    4683830, -1118012, 562801, 1941362, -1738462, // seg 8
    4501658, -2508250, 2050629, -6109091, 5691989, // seg 9
};

static const int eq_filt_24000[] = {
    4194304, -4155270, 4155270, 8349396, -8349396, // seg 0
    4194304, -4111983, 4111983, 8305488, -8305488, // seg 1
    4376704, -4071932, 3727436, 8259166, -7914963, // seg 2
    4194304, -3834222, 3834222, 8012684, -8012684, // seg 3
    4639059, -3677958, 2923231, 7781371, -7035358, // seg 4
    4194304, -2798309, 2798309, 6713940, -6713940, // seg 5
    5226562, -2527653, 1200052, 5744245, -4609744, // seg 6
    6089881, -1588180, -211714, 871670, -600348, // seg 7
    4591270, -1699680, 1190081, -4319697, 3946212, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_12000[] = {
    4194304, -4116600, 4116600, 8310192, -8310192, // seg 0
    4194304, -4031278, 4031278, 8222415, -8222415, // seg 1
    4553560, -3953280, 3310472, 8119719, -7479109, // seg 2
    4194304, -3505031, 3505031, 7638623, -7638623, // seg 3
    5023672, -3231436, 2005506, 7084777, -5915137, // seg 4
    4194304, -1869612, 1869612, 5116528, -5116528, // seg 5
    5740676, -1697581, 110478, 2713270, -1982394, // seg 6
    4194304, 0, 0, 0, 0, // seg 7
    4194304, 0, 0, 0, 0, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_32000[] = {
    4194304, -4164995, 4164995, 8359198, -8359198, // seg 0
    4194304, -4132411, 4132411, 8326265, -8326265, // seg 1
    4331622, -4102178, 3839170, 8292490, -8029609, // seg 2
    4194304, -3921237, 3921237, 8106532, -8106532, // seg 3
    4533629, -3800359, 3201989, 7942698, -7348217, // seg 4
    4194304, -3096263, 3096263, 7126655, -7126655, // seg 5
    5024252, -2854296, 1689949, 6465583, -5397544, // seg 6
    5933636, -1802992, 45000, 2907775, -2055417, // seg 7
    4698276, -1027225, 467125, -1161043, 1036501, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_16000[] = {
    4194304, -4135890, 4135890, 8329792, -8329792, // seg 0
    4194304, -4071431, 4071431, 8263944, -8263944, // seg 1
    4465828, -4012139, 3513185, 8190651, -7692657, // seg 2
    4194304, -3665937, 3665937, 7825357, -7825357, // seg 3
    4838675, -3446210, 2428716, 7442507, -6451381, // seg 4
    4194304, -2286138, 2286138, 5904200, -5904200, // seg 5
    5537796, -2025145, 516282, 4246812, -3216518, // seg 6
    5895624, -1855253, 109513, -3205330, 2280357, // seg 7
    4194304, 0, 0, 0, 0, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

static const int eq_filt_08000[] = {
    4194304, -4078289, 4078289, 8270999, -8270999, // seg 0
    4194304, -3952156, 3952156, 8139403, -8139403, // seg 1
    4724830, -3838374, 2936428, 7970910, -7075899, // seg 2
    4194304, -3204077, 3204077, 7267335, -7267335, // seg 3
    5351775, -2850518, 1326878, 6324008, -4956265, // seg 4
    4194304, -1265128, 1265128, 3603950, -3603950, // seg 5
    5870556, -1487880, -134585, -383507, 274002, // seg 6
    4194304, 0, 0, 0, 0, // seg 7
    4194304, 0, 0, 0, 0, // seg 8
    4194304, 0, 0, 0, 0, // seg 9
};

const int *get_pcm_eq_tab(u32 sr)
{
    const int *sr_tab = &eq_filt_32000[0];
    switch (sr) {
    case 8000:
        sr_tab =  &eq_filt_08000[0];
        break;
    case 12000:
        sr_tab =  &eq_filt_12000[0];
        break;
    case 16000:
        sr_tab =  &eq_filt_16000[0];
        break;
    case 24000:
        sr_tab =  &eq_filt_24000[0];
        break;
    case 32000:
        sr_tab =  &eq_filt_32000[0];
        break;
    default:
        break;
    }
    return sr_tab;
}

#endif








